package com.oscar.ehome.view.teamview.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.bean.messageBean.Contact;
import com.oscar.ehome.bean.teamBean.CusourTeamBean;
import com.oscar.ehome.bean.teamBean.TeamListBean;
import com.oscar.ehome.provider.gildeimage.GildeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


/**
 * Created by zhangxutong .
 * Date: 16/08/28
 */

public class TeamAdapter extends BaseAdapter {
    protected Context mContext;
    protected List<TeamListBean.ListBean> mDatas;
    //排序的列表
    List<CusourTeamBean> list = new ArrayList<>();
    protected LayoutInflater mInflater;
    IntentListener intentListener;


    public TeamAdapter(Context context, List<TeamListBean.ListBean> mDatas, List<CusourTeamBean> list) {
        this.mContext = context;
        this.mDatas = mDatas;
        this.list = list;
        mInflater = LayoutInflater.from(mContext);
    }

    public interface IntentListener {
        public void onIntentClick(int postion, int team_type);
    }

    public void setOnINtentClickListener(IntentListener intentListener) {
        this.intentListener = intentListener;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = View.inflate(parent.getContext(), R.layout.adapter_teamlist_item_city, null);
            vh.team_item_name = (TextView) convertView.findViewById(R.id.team_item_name);
            vh.team_item_imghead = (ImageView) convertView.findViewById(R.id.team_item_imghead);
            vh.team_item_level = (TextView) convertView.findViewById(R.id.team_item_level);
            vh.team_item_up = (TextView) convertView.findViewById(R.id.team_item_up);
            vh.team_item_down = (TextView) convertView.findViewById(R.id.team_item_down);
            vh.content = convertView.findViewById(R.id.content);
            vh.tv_word = (TextView) convertView.findViewById(R.id.tv_word);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();

        }
        String word = list.get(position).getHeaderWord();
        vh.tv_word.setText(word);

        vh.team_item_name.setText(mDatas.get(position).getName());
        String photo = C.qiniu + mDatas.get(position).getPhoto();
//        GildeUtils.imgLoadPic(mContext, photo, vh.team_item_imghead, "net-p");
        GildeUtils.loadUrlImage4(photo, vh.team_item_imghead, mContext);

        vh.team_item_up.setText("直属下级:" + mDatas.get(position).getChildNum());
        vh.team_item_down.setText("全部下级:" + mDatas.get(position).getAllChildNum());
        vh.team_item_level.setText(mDatas.get(position).getRoleMobile().getRoleName());

        //将相同字母开头的合并在一起
        if (position == 0) {
            //第一个是一定显示的
            vh.tv_word.setVisibility(View.VISIBLE);
        } else {
            //后一个与前一个对比,判断首字母是否相同，相同则隐藏
            String headerWord = list.get(position - 1).getHeaderWord();
            if (word.equals(headerWord)) {
                vh.tv_word.setVisibility(View.GONE);
            } else {
                vh.tv_word.setVisibility(View.VISIBLE);
            }
        }

        vh.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentListener.onIntentClick(position, 1);
            }
        });

        vh.team_item_imghead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentListener.onIntentClick(position, 2);
            }
        });
        return convertView;
    }

    public class ViewHolder {
        TextView team_item_name;
        ImageView team_item_imghead;
        View content;
        TextView team_item_level;
        TextView team_item_up;
        TextView team_item_down;
        TextView tv_word;
    }

}
